package jsyf.oa.service.crm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.DateUtils;
import jsyf.oa.dao.crm.CustomerVisitDao;
import jsyf.oa.dao.system.UserDao;
import jsyf.oa.dao.wreport.WReportCusVisitDao;
import jsyf.oa.dao.wreport.WReportCustomerDao;
import jsyf.oa.entity.crm.CustomerVisitEntity;
import jsyf.oa.entity.wreport.WReportCusVisitEntity;
import jsyf.oa.entity.wreport.WReportCustomerEntity;
import jsyf.oa.facade.crm.CustomerVisitFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by wangmin on 2018/1/15.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class CustomerVisitService implements CustomerVisitFacade {
    @Autowired
    private CustomerVisitDao customerVisitDao;
    @Autowired
    private WReportCustomerDao wReportCustomerDao;
    @Autowired
    private WReportCusVisitDao wReportCusVisitDao;

    public CustomerVisitEntity getEntity(String strJSON) {
        CustomerVisitEntity entity = JSON.toJavaObject(JSON.parseObject(strJSON), CustomerVisitEntity.class);
        return entity;
    }

    @Override
    public Integer getStateByUserId(String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        return customerVisitDao.getStateByUserId(jsonObject.get("userId").toString());
    }

    @Override
    public JSONObject getEntityByUserId(String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        return customerVisitDao.getEntityByUserId(jsonObject.get("userId").toString());
    }

    @Override
    public Integer save(String jsonData) {
        return customerVisitDao.saveEntity(getEntity(jsonData));
    }

    @Override
    public Integer updateEntity(String jsonData) {
        CustomerVisitEntity customerVisitEntity = getEntity(jsonData);
        WReportCustomerEntity wReportCustomerEntity = wReportCustomerDao.getByTel(customerVisitEntity.getCustomerTel());
        if (wReportCustomerEntity == null) {
            wReportCustomerEntity = new WReportCustomerEntity();
            wReportCustomerEntity.setName(customerVisitEntity.getCustomerName());
            wReportCustomerEntity.setTel(customerVisitEntity.getCustomerTel());
            wReportCustomerEntity.setCreateUserId(customerVisitEntity.getCreateUserId());
            wReportCustomerEntity.setSource("销售录入");
            wReportCustomerDao.add(wReportCustomerEntity);

            wReportCustomerEntity = wReportCustomerDao.getByTel(customerVisitEntity.getCustomerTel());
        }

        WReportCusVisitEntity wReportCusVisitEntity = new WReportCusVisitEntity();
        wReportCusVisitEntity.setReportDate(DateUtils.getBeforeDate(new Date(), 0));
        wReportCusVisitEntity.setCreateUserId(customerVisitEntity.getCreateUserId());
        wReportCusVisitEntity.setCustomerId(wReportCustomerEntity.getId());
        wReportCusVisitEntity.setVisitMode("1");
        wReportCusVisitDao.add(wReportCusVisitEntity);

        return customerVisitDao.updateEntity(customerVisitEntity);
    }

    @Override
    public Integer addCustomerVisit(String jsonData) {
        CustomerVisitEntity customerVisitEntity = getEntity(jsonData);
        WReportCustomerEntity wReportCustomerEntity = wReportCustomerDao.getByTel(customerVisitEntity.getCustomerTel());
        if (wReportCustomerEntity == null) {
            wReportCustomerEntity = new WReportCustomerEntity();
            wReportCustomerEntity.setTel(customerVisitEntity.getCustomerTel());
            wReportCustomerEntity.setCreateUserId(customerVisitEntity.getCreateUserId());
            wReportCustomerEntity.setSource("销售录入");
            wReportCustomerDao.add(wReportCustomerEntity);

            wReportCustomerEntity = wReportCustomerDao.getByTel(customerVisitEntity.getCustomerTel());
        }

        WReportCusVisitEntity wReportCusVisitEntity = new WReportCusVisitEntity();
        wReportCusVisitEntity.setReportDate(DateUtils.getBeforeDate(new Date(), 0));
        wReportCusVisitEntity.setCreateUserId(customerVisitEntity.getCreateUserId());
        wReportCusVisitEntity.setCustomerId(wReportCustomerEntity.getId());
        wReportCusVisitEntity.setVisitMode("0");
        return  wReportCusVisitDao.add(wReportCusVisitEntity);
    }
}
